const txtLoginId = new FiledValidator('txtLoginId', async function (val) {
    if (!val.trim()) {
        return '账号不为空'
    }
    const resp = await API.exists(val);
    if (resp.data) {
        return '账号已存在'
    }
})

const txtLoginPwd = new FiledValidator('txtLoginPwd', async function (val) {
    if (!val.trim()) {
        return '密码不为空'
    }
})

const txtNickname = new FiledValidator('txtNickname', async function (val) {
    if (!val.trim()) {
        return '昵称不为空'
    }
})

const txtLoginPwdConfirm = new FiledValidator('txtLoginPwdConfirm', async function (val) {
    if (!val.trim()) {
        return '密码不为空'
    } else if (val !== txtLoginPwd.input.value) {
        return '密码不一致'
    }
})

const form = $('.user-form')

form.onsubmit = async function (e) {
    e.preventDefault();
    console.log('1')
    const result = await FiledValidator.validate(txtLoginId,
        txtLoginPwd,
        txtNickname,
        txtLoginPwdConfirm);
    if (!result) {
        return;
    }
    const formData = new FormData(form);
    const data = Object.fromEntries(formData.entries());

    const resp = await API.reg(data);
    console.log(resp)
    if (resp.code === 0) {
        alert('账号注册成功');
        location.href = './login.html'
    }
}